package in.muton.edushare.pages.batch;

import in.muton.edushare.auth.roles.DeptRoleCheck;
import in.muton.edushare.pages.EduShareBasePage;
import in.muton.edushare.pages.curricula.CurriculaSummary;
import in.muton.edushare.pages.deptpages.DeptBatches;
import in.muton.edushare.pages.deptpages.DeptPage;
import in.muton.edushare.pages.univ.UnivBatches;
import in.muton.edushare.pages.univ.UnivPage;
import in.muton.edushare.processing.objects.UnivDataHandler;
import in.muton.edushare.url.EntityPageParams;
import in.muton.edushare.url.URLLoader;
import in.muton.edushare.wicket.models.CayenneModel;

import org.apache.wicket.PageParameters;
import org.apache.wicket.RestartResponseException;
import org.apache.wicket.behavior.AttributeAppender;
import org.apache.wicket.markup.html.CSSPackageResource;
import org.apache.wicket.markup.html.WebMarkupContainer;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.link.BookmarkablePageLink;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.Model;

import com.edushare.common.constants.URLConstants;
import com.edushare.common.dao.BatchDAO;
import com.edushare.common.util.StringUtil;
import com.edushare.common.vo.Batches;
import com.edushare.common.vo.Departments;
import com.edushare.common.vo.Universities;

public abstract class BatchBase extends EduShareBasePage {

	protected IModel<Batches> batchModel;
	private String[] menuItems = { "batchDetails", "batchEdit" };
	public static final int fromUnivPage = 1;
	public static final int fromDeptPage = 2;
	protected int fromPage = 2;

	public BatchBase(PageParameters params) {
		String batchId = params.getString("0");
		fromPage = params.getAsInteger("source", fromDeptPage);

		if (StringUtil.isValid(batchId)) {
			Batches b = new BatchDAO(getSessionORMInterface())
					.getBatchesById(Integer.parseInt(batchId));
			if (b == null)
				show404();
			this.batchModel = new CayenneModel<Batches>(b);
		} else {
			show404();
		}
		init();
	}

	public BatchBase(IModel<Batches> batchModel) {
		if (batchModel == null || batchModel.getObject() == null)
			show404();
		this.batchModel = batchModel;
		init();
	}

	private void init() {
		add(CSSPackageResource.getHeaderContribution(
				in.muton.edushare.pages.assignment.AssignmentBase.class,
				"assnHome.css"));
		add(new Label("batchName", batchModel.getObject().getBatchName()));

		PageParameters pageParams = new PageParameters();
		pageParams.put("0", Integer.toString(batchModel.getObject()
				.getCurricula().getCurriculaId()));
		Link<Void> l = null;
		add(l = new BookmarkablePageLink<Void>("currLink",
				CurriculaSummary.class, pageParams));
		l.add(new Label("currName", batchModel.getObject().getCurricula()
				.getCurriculaName()));
		setupMenu();
	}

	private void setupMenu() {
		String highlight = getActivePage();

		for (String menuname : menuItems) {
			WebMarkupContainer wmc = new WebMarkupContainer(menuname);
			if (menuname.equals(highlight)) {
				wmc.add(new AttributeAppender("class", true, new Model<String>(
						"es-selected"), " "));
			}

			if (menuname.equals("batchDetails")) {
				wmc.add(new Link<Void>("link") {
					private static final long serialVersionUID = -3450973993675246085L;

					@Override
					public void onClick() {
						PageParameters params = new PageParameters();
						params.add("0", ""
								+ batchModel.getObject().getBatchId());
						setResponsePage(BatchSummary.class, params);
					}
				});
			} else if (menuname.equals("batchEdit")) {
				wmc.add(new Link<Void>("link") {
					private static final long serialVersionUID = -1956645345321723467L;

					@Override
					public void onClick() {
						setResponsePage(new BatchEditPage(batchModel));
					}

					@Override
					public boolean isVisible() {
						DeptRoleCheck drc = new DeptRoleCheck(batchModel
								.getObject().getCurricula().getDepartment(),
								getLoggedInUser());
						if (drc.isPrimaryAdmin() || drc.isUnivAdmin())
							return true;
						return false;
					}
				});
			}
			add(wmc);
		}

		// Redirection links
		if (fromPage == fromUnivPage) {
			add(new BookmarkablePageLink<Void>("backToList", UnivBatches.class,
					getUnivPagesParamaters()));
		} else {
			add(new BookmarkablePageLink<Void>("backToList", DeptBatches.class,
					getDeptPagesParamaters()));
		}

		add(new BookmarkablePageLink<Void>("backToDept", DeptPage.class,
				getDeptPagesParamaters()));
		add(new BookmarkablePageLink<Void>("backToUniv", UnivPage.class,
				getUnivPagesParamaters()));
		PageParameters params = new PageParameters();
		params.add("0", "" + batchModel.getObject().getBatchId());
		DeptRoleCheck drc = new DeptRoleCheck(batchModel
				.getObject().getCurricula().getDepartment(),
				getLoggedInUser());
		boolean visible = drc.isPrimaryAdmin() || drc.isUnivAdmin();
		add(new BookmarkablePageLink<Void>("batchGrade", BatchGradeStudentList.class,
				params).setVisible(visible));
	}

	private void show404() {
		new RestartResponseException(URLLoader
				.getPageClass(URLConstants.PAGE_NOT_FOUND_PATH));
	}

	@Override
	public String getUserRole() {
		return " : "
				+ new UnivDataHandler(new CayenneModel<Universities>(batchModel
						.getObject().getCurricula().getUniversity()))
						.getUserRole(getLoggedInUser());
	}

	public PageParameters getUnivPagesParamaters() {
		return EntityPageParams.getParams(new CayenneModel<Universities>(
				batchModel.getObject().getCurricula().getUniversity()));
	}

	public PageParameters getDeptPagesParamaters() {
		return EntityPageParams.getParams(new CayenneModel<Departments>(
				batchModel.getObject().getCurricula().getDepartment()));
	}

	public abstract String getActivePage();

}
